package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrSequenceOf;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.nio.ByteBuffer;

// Assuming the existence of similar classes in Java for AxdrType, ReverseByteArrayInputStream, and ReverseByteArrayOutputStream
// and that SETROADDAR and PIID are already translated to Java.


public class SetThenGetResponseNormalList extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piid_acd = null;
    private SubSeqOfReads reads = null;

    public SetThenGetResponseNormalList() {
    }

    public void set_all(PIID piid_acd, SubSeqOfReads reads) {
        this.piid_acd = piid_acd;
        this.reads = reads;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid_acd = new PIID();
        // Assuming PIID has a method setNumBit
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);
        this.reads = new SubSeqOfReads();
        codeLength += this.reads.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.reads != null && this.piid_acd != null) {
                codeLength = 0;
                codeLength += this.reads.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new RuntimeException("piid_acd or reads is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        // Assuming ReverseByteArrayOutputStream has a method setBufSize
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.piid_acd != null && this.reads != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", reads: " + this.reads + "}";
        } else {
            return "piid_acd or reads is null";
        }
    }

    public class SubSeqOfReads extends AxdrSequenceOf<SETROADDAR> {
        @Override
        public SETROADDAR createListElement() {
            return new SETROADDAR();
        }
    }
}



